/*
 * @Author: your name
 * @Date: 2022-01-22 14:05:46
 * @LastEditTime: 2022-01-28 00:44:57
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \json-serveg:\桌面2\VueStudy\vue-ego\vue-ego\src\router\index.js
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Layout from '../views/Layout'
import Login from '../views/Login/Login.vue'
import Home from '../views/Home/Home.vue'
import store from '../store/index'

//异步导入组件
const Goods = ()=> import('../views/Goods/Goods.vue')
const Params = ()=> import('../views/Params/Params.vue')
const Arguments = ()=> import('../views/Params/Arguments/Arguments.vue')
const Adver = ()=> import('../views/Adver/Adver.vue')
const Order = ()=> import('../views/Order/Order.vue')
const OrderList = ()=> import('../views/Order/OrderList/OrderList.vue')
const OrderBack = ()=> import('../views/Order/OrderBack/OrderBack.vue')
const AddGoods = () => import('../views/Goods/AddGoods.vue')

Vue.use(VueRouter)

const routes = [
  {
    path:'',
    component:Layout,
    meta:{isLogin:true}, // 路由元信息
    children:[
      {
        path:'/',
        name:'Home',
        component:Home
      },
      {
        path:'/goods',
        name:'Goods',
        component:Goods
      },
      {
        path:'/add-goods',
        name:'AddGoods',
        component:AddGoods
      },
      {
        path:'/params',
        name:'Params',
        component:Params,
        redirect:'/params/arguments',
        children:[
          {
            path:'arguments',
            name:'Arguments',
            component:Arguments
          }
        ]

      },
      {
        path:'/adver',
        name:'Adver',
        component:Adver
      },
      {
        path:'/order',
        name:'Order',
        component:Order,
        redirect:'/order/order-list', //重定向
        children:[
          {
            path:'order-list',
            name:'OrderList',
            component:OrderList
          },
          {
            path:'order-back',
            name:'OrderBack',
            component:OrderBack
          },
        ]
      },
    ]
  },
  {
    path:'/login',
    component:Login
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


//路由拦截
// 全局前置守卫
router.beforeEach((to,from,next) => {
  // console.log(to,from);
  //判断是否需要登录
  if (to.matched.some(record => record.meta.isLogin)) { // some判断数组的方法,有值就返回true
    //判断用户是否已经登录
    let token= store.state.loginModule.userinfo.token
    if (token) {
      next()
    }else{
      next('/login')
    }



  }else{//没有规则不需要登录
    next()
  }
  
})

export default router
